package component;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import play.mvc.Http.Request;
import play.templates.JavaExtensions;


/**
 * 分页标签辅助类
 * @author think
 *
 */
/**
 * @author Administrator
 *
 */
public class Page  implements Serializable{

	private static final long serialVersionUID = 1L;
	/**
	 * 当前页
	 */
	public int currPage;
	/**
	 * 总记录数
	 */
	public int totalCount;
	/**
	 * 总页数
	 */
	public int totalPageCount;
	/**
	 * 每页的记录条数
	 */
	public int pageSize;
	/**
	 * 显示分页的页码数
	 */
	public int showPageCount = 10;
	/**
	 * 风格
	 */
	public int style = 1;
	/**
	 * 页面显示的js方法
	 */
	public String funMethod;
	/**
	 * 查询条件
	 */
	public String conditions; 
	
	public int getTotalPageCount () {
		return (this.totalCount - 1) / this.pageSize + 1;
	}
	
	/**
	 * 进行分页块的显示
	 * @return
	 */
	public String getThemeNumber() {
		return getDefaultTheme();
	}
	
	
	/**
 	* <div class="page">
	* <span class="fl pr15 ml150">第1页/共3页（共25条记录）</span>
	* <a href="#">
	* <a class="page-link on" href="#">1</a>
	* <a class="page-link" href="/financing/sbtz/grlb/2">2</a>
	* <a class="page-link" href="/financing/sbtz/grlb/3">3</a>
	* <a class="page-link next" href="/financing/sbtz/grlb/2"></a>
	* <a class="page-link" href="/financing/sbtz/grlb/3">尾页</a>
	* </div>
	* @return
	* @author yanghao
	* 2015年6月10日 下午1:47:06
	*/
	private String getDefaultTheme(){
		StringBuffer strBuff = new StringBuffer("<div class=\"page\">");
		String myUrl = Request.current().actionMethod;
//		String myUrl = "action";
		if(totalPageCount == 1) {
			strBuff.append("<span style=\"margin-left: auto;\"  class=\"fl pr15 ml150\">第"+currPage+"页").append("/共"+totalPageCount+"页")
			.append("（共").append(totalCount).append("条记录）</span>").append("</div>");
			return strBuff.toString();
		}
		
		if(currPage <= 1) {
			currPage = 1;
		}
		
		if(currPage >= totalPageCount) {
			currPage = totalPageCount;
		}
		
		int nextPage = currPage+1 > totalPageCount ? totalPageCount : currPage + 1;
		String dwonPage = myUrl + "?currPage=" + nextPage + "&pageSize=" + pageSize+"&"+conditions;
		
		
		int lastPage = currPage - 1 < 1 ? 1  :currPage - 1;
		String upPage = myUrl +"?currPage=" + lastPage + "&pageSize=" + pageSize+"&"+conditions;
		strBuff.append("<span style=\"margin-left: auto;\" class=\"fl pr15 ml150\">第"+currPage+"页").append("/共"+totalPageCount+"页")
		.append("（共").append(totalCount).append("条记录）</span>");
		
		String homeUrl = myUrl + "?currPage=1&pageSize="+pageSize+"&"+conditions;
		String notPage =  myUrl + "?currPage="+totalPageCount+"&pageSize="+pageSize+"&"+conditions;
		
		if(currPage != 1){
			strBuff.append("<a class=\"page-link\" href=\""+homeUrl+"\">首页</a><a class=\"page-link prev\" href=\""+upPage+"\"><</a>");
		}
		
		if(totalPageCount < showPageCount){
			showPageCount = totalPageCount;
		}
		
		/*if(showPageCount != 2){
			int i = 1,sum = 0;
			int half = showPageCount/2;
			if(currPage > half){
				i = currPage - half;
			}
			if(totalPageCount - i < showPageCount){//如果总页数减去起始位置小于 要显示的页码数
				i = totalPageCount - showPageCount + 1;
			}
			for(; i <=totalPageCount; i++ ){
				if(sum == showPageCount){
					break;
				}
				sum ++;
				strBuff.append("<a class=\"page-link\" href=\""+myUrl+"?currPage="+i+"&pageSize="+pageSize+"&"+conditions+"\" >"+i+"</a>&nbsp;");
			}
		}else{
			int tempCurrPage = currPage;
			if(currPage == 1){
				lastPage = 1;
				nextPage = 2;
			}else{
				if(currPage == totalPageCount){
					tempCurrPage = totalPageCount - 1;
				}
			}
			strBuff.append("<a class=\"page-link\" href=\""+myUrl+"?currPage="+lastPage+"&pageSize="+pageSize+"&"+conditions+"\">"+tempCurrPage+"</a>");
			strBuff.append("<a class=\"page-link\" href=\""+myUrl+"?currPage="+nextPage+"&pageSize="+pageSize+"&"+conditions+"\">"+(tempCurrPage+1)+"</a>");
		}*/
		final int pageNoCount = 5;//页码数量
		if(showPageCount<=pageNoCount){
			for(int i=1;i<=showPageCount;i++){
				if(i==currPage){
					strBuff.append("<a class=\"page-link on\" style='color:red;' href=\""+myUrl+"?currPage="+i+"&pageSize="+pageSize+"&"+conditions+"\" >"+i+"</a>&nbsp;");
				}else{
					strBuff.append("<a class=\"page-link\" href=\""+myUrl+"?currPage="+i+"&pageSize="+pageSize+"&"+conditions+"\" >"+i+"</a>&nbsp;");
				}
			}
		}else{
			//中间高亮风格
			int mid = pageNoCount/2, start, end;
			if(currPage>totalPageCount-mid)
				start = totalPageCount-pageNoCount+1;
			else
				start = currPage-mid < mid ? 1 : currPage-mid;
			if(currPage<pageNoCount-mid)
				end = pageNoCount;
			else
				end = currPage+mid > totalPageCount ? totalPageCount : currPage+mid;

			for(int i=start;i<=end;i++){
				if(i==currPage){
					strBuff.append("<a class=\"page-link on\" style='color:red;' href=\""+myUrl+"?currPage="+i+"&pageSize="+pageSize+"&"+conditions+"\" >"+i+"</a>&nbsp;");
				}else{
					strBuff.append("<a class=\"page-link\" href=\""+myUrl+"?currPage="+i+"&pageSize="+pageSize+"&"+conditions+"\" >"+i+"</a>&nbsp;");
				}
			}
		}
		
		
		if(currPage != totalPageCount){
			strBuff.append("<a class=\"page-link next\" href=\""+dwonPage+"\">></a>&nbsp;<a class=\"page-link\" href=\""+notPage+"\">尾页</a>");
		}
		StringBuilder jumpPageJS = new StringBuilder()
		.append("(function(){var ev=event||window.event;")
		.append("if(ev.type=='click'||ev.keyCode==13){")
		.append("var v=document.getElementById('jumpPageNo').value;if(!(v&&/^[1-9]+[0-9]*]*$/.test(v)")
		.append("&&v<=").append(totalPageCount).append("))v=1;")
		.append(String.format("location.href='%s?currPage='+v+'&pageSize=%s%s'",
					myUrl, pageSize, StringUtils.isNotBlank(conditions) ? "&"+JavaExtensions.raw(conditions) : ""))
		.append("}})()");
		
		String inputText = " <input type=\"text\" style=\"border: 1px solid #9ab1e5;color: #386aaf;display: inline-block;float: left;"
				+ "height: 21px;margin-top: 5px;text-align: center;width: 30px;\" size=4 value=\"\" id=\"jumpPageNo\" onkeypress=\"" 
				+ jumpPageJS + "\"/> <a style=\"cursor:pointer;\" onclick=\"" + jumpPageJS + "\" class=\"page-link\" id=\"jumpPage\">跳转</a>";
		strBuff.append(inputText);
		strBuff.append("</div>");
		strBuff.append("<input type=\"hidden\" id=\"myurl\" value=\""+myUrl+"\"/>");
		strBuff.append("<input type=\"hidden\" id=\"pageSize\" value=\""+pageSize+"\"/>");
		strBuff.append("<input type=\"hidden\" id=\"conditions\" value=\""+conditions+"\"/>");
		return strBuff.toString();
	}

	public int[] getRount(int showPageCount,int totalPageCount,int currPage){
        int[] inter = new int[2];//定义一个具有两个元素的数组
        int rount = totalPageCount - showPageCount + 1;
        if(rount > 0){
            if(rount > showPageCount){
                if(currPage >= showPageCount && currPage <= rount){
                    inter[0] = currPage;
                    inter[1] = showPageCount + currPage - 1;
                }else if(currPage > rount && currPage <= totalPageCount){
                    inter[0] = rount;
                    inter[1] = totalPageCount;
                }else{
                    inter[0] = 1;
                    inter[1] = showPageCount;
                }
            }else{
                if(currPage >= showPageCount){
                    inter[0] = rount;
                    inter[1] = totalPageCount;
                }else{
                    inter[0] = 1;
                    inter[1] = showPageCount;
                }
            }
        }else{
            inter[0] = 1;
            inter[1] = showPageCount;
        }
        return inter;
    }
	
	public static void main(String[] args) {
		Page page = new Page();
		page.currPage = 5;
		page.totalCount = 200;
		page.totalPageCount = 10;
		page.pageSize = 40;
		
		page.conditions="b=3&c=4";
		page.funMethod="method";
	}
}
